package com.rui.study.micro.goods.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

public class SimpleApplicationInitializer implements ApplicationContextInitializer {

    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleApplicationInitializer.class);

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("容器初始化>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(applicationContext.getBeanDefinitionCount());

        Environment environment = applicationContext.getEnvironment();
        print(environment, new String[]{
                "eureka.client.serviceUrl.defaultZone",
                "eureka.instance.prefer-ip-address",
        });
        for (String s : environment.getActiveProfiles()) {
            System.out.println(s);
        }
    }

    public void print(Environment environment, String[] properties) {
        StringBuffer sb = new StringBuffer("Properties Watching:\r\n");
        for (String p : properties) {
            sb.append(String.format("%s = %s\r\n", p, environment.getProperty(p)));
        }
        LOGGER.info(sb.toString());
    }
}
